import Vue from "vue"
import VueRouter from "vue-router"
import Home from "@/pages/Home"
import Login from "@/pages/Login"
import Register from "@/pages/Register"
import Search from "@/pages/Search"
import Test from "@/pages/Test"

Vue.use(VueRouter)

//保存 VueRouter.prototype当中的push
let orginPush = VueRouter.prototype.push

//把VueRouter.prototype的push重新更改  组件中使用的是自己写的一个push
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve === undefined && reject === undefined) {
    //成功和失败的回调都没传
    //普通函数调用的  this==>window 人家原先的应该指路由器实例化对象
    //如果都没传那么调用人家原先的push时手动catch
    return orginPush.call(this, location).catch(() => {})
  } else {
    //可能传了一个 可能传了两个
    return orginPush.call(this, location, resolve, reject)
  }
}

export default new VueRouter({
  routes: [
    {
      path: "/home",
      component: Home,
    },
    {
      path: "/login",
      component: Login,
      meta: {
        //元数据
        isHidden: true,
      },
    },
    {
      path: "/register",
      component: Register,
      meta: {
        //元数据
        isHidden: true,
      },
    },
    {
      path: "/search/:keyword?",
      component: Search,
      name: "search",
      props: (route) => ({
        ...route.params,
        ...route.query,
      }),
    },

    {
      path: "/test",
      component: Test,
    },
    {
      path: "/",
      redirect: "/home",
    },
  ],
})
